<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Products extends Controller_Admin_Base
{
    public function action_index()
    {
        Request::current()->redirect(Helper_Admin::get_url() . '/products/reviews'); 
    }
    
    public function action_reviews()
    {    
        $category_id = $this->request->param('id') ? $this->request->param('id') : 0;
        //$page        = $this->request->param('page') ? $this->request->param('page') : 1;
        
        $content = new View('admin/products/reviews_tpl');
        if ($category_id) {
            $category = ORM::factory('category', $category_id);
            $content->categories = $category->get_categories();
            $products            = ORM::factory('product')->get_products($category, true);
            $content->breadcrumbs = $category->get_breadcrumbs();
            $content->category   = $category;
        } else {
            $content->categories = ORM::factory('category')->get_top_categories();
            $products            = ORM::factory('product')->get_products(0, true);
            $content->breadcrumbs = null;
        } 
        $content->products = $products;
        $content->models   = ORM::factory('model')->extract_models($products);     
        $this->print_template('Товары и отзывы', $content); 
    }
    
    /* TODO */
    public function action_photos()
    {
        
    }
    
    public function action_unsorted()
    {    
        $category_id = $this->request->param('id') ? $this->request->param('id') : 0;
        //$page        = $this->request->param('page') ? $this->request->param('page') : 1;
        
        $content = new View('admin/products/unsorted_tpl');
        if ($category_id) {
            $category = ORM::factory('category', $category_id);
            $content->categories = $category->get_categories();
            $products            = ORM::factory('product')->get_products($category, 0);
            $content->breadcrumbs = $category->get_breadcrumbs();
            $content->category   = $category;
        } else {
            $content->categories = ORM::factory('category')->get_top_categories();
            $products            = ORM::factory('product')->get_products(0, 0);
            $content->breadcrumbs = null;
        } 
        $content->products = $products;
        $content->models   = ORM::factory('model')->extract_models($products);     
        $this->print_template('Неотсортированные товары', $content);
        
        
        /*$category_id = $this->request->param('id') ? $this->request->param('id') : 0;
        $page        = $this->request->param('page') ? $this->request->param('page') : 1;
        
        $content = new View('admin/products/unsorted_tpl');
        $products =  ORM::factory('product')->get_products(null, 0, $page);
        $content->categories = ORM::factory('category')->extract_categories($products);
        $content->products   = $products;
        $content->count      = ORM::factory('product')->get_products(null, 0, null, true);
        $content->pagination = array(
                'page' => $page,
                'per_page' => ORM::factory('product')->get_products_per_page(),
        );
       
        $this->print_template('Неотсортированные товары', $content); */
    }
    
    public function action_sort()
    {
        $product_id = $this->request->param('id') ? $this->request->param('id') : 0; 
        $prev_id    = $this->request->param('prev') ? $this->request->param('prev') : 0;
        
        $product = ORM::factory('product', $product_id);
        $next_product = ORM::factory('product')->get_next_unsorted_product($product);
        $market_url = $model_name = null;
        
        $prev_product = $prev_model = null;
        $search_models = null;
        if ($prev_id) {
            $prev_product = ORM::factory('product', $prev_id);
            if ($prev_product->model_id) {
                $prev_model = ORM::factory('model', $prev_product->model_id);
            }   
        } 
        
        if (HTTP_Request::POST == $this->request->method()) {
            $need_redirect = true;
            $action = $this->request->post('action');
            if ($action == 'add_model') {
                $name        = $this->request->post('name');
                $market_url  = $this->request->post('market_url');
                $model = ORM::factory('model')->add_model($name, $market_url);    // $product->clean_name
                $product->to_model($model);    
            } else if ($action == 'choose_model') {
                $model_id = $this->request->post('model_id'); 
                $model = ORM::factory('model', $model_id);
                $product->to_model($model);
            } else if ($action == 'find_market') {
                $name = $this->request->post('name');
                $market_url = Model_ReviewParser_YandexMarket::instance()->get_reviews_url($name);
                $model_name = null;
                if ($market_url) {
                    $model_name = Model_ReviewParser_YandexMarket::instance()->get_model_name($market_url);
                }   
                $need_redirect = false;          
            } else if ($action == 'search_model') {
                $name = $this->request->post('name');
                $search_models = ORM::factory('model')->simple_search($name);  
                $need_redirect = false;  
            }
            if ($need_redirect) {
                if ($next_product) {
                    Request::current()->redirect(Helper_Admin::get_url() . '/products/sort/' . $next_product->id . '/prev/' . $product->id);
                } else {
                    Request::current()->redirect(Helper_Admin::get_url() . '/products/sort/' . $product->id . '/prev/' . $product->id);    
                }
            }
        }
        
        // Пытаемся получить потенциальные модели снова (после добавления товаров),
        // т.к. во время ручного соотношения по моделям уже могли появиться нужные варианты 
        $model   = ORM::factory('model')->calculate_model($product);
        
        $content = new View('admin/products/sort_tpl');
        $content->product = $product;
        $content->models  = ORM::factory('model')->get_potential_models($product);
        $content->next_product = $next_product;
        $content->prev_product = $prev_product;
        $content->prev_model   = $prev_model;
        $content->market_url   = $market_url;
        $content->model_name   = $model_name; 
        $content->search_models = $search_models;  
        $this->print_template('Обработка товара', $content);
    }
} 
